﻿using System;
using System.Collections.Generic;
using System.Text;
using xReplace;

namespace xReplaceCmd
{
    class Program
    {
        static void Main(string[] args)
        {
            string projectFilename = "";
            string overrideSourcePath = "";
            string overrideDestinationPath = "";
            string overrideSingleFile = "";
            InvocationOptions options = new InvocationOptions();

            //loop params here
            try
            {
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i].ToLower().Trim().StartsWith("/inpath"))
                    {
                        i++;
                        options.SetOption(InvocationOptions.InvocationSwitch.overrideSourcePath,args[i]);
                    }else 
                    if (args[i].ToLower().Trim().StartsWith("/outpath"))
                    {
                        i++;
                        options.SetOption(InvocationOptions.InvocationSwitch.overrideDestinationPath, args[i]);
                    }
                    else
                    if (args[i].ToLower().Trim().StartsWith("/singlefile"))
                    {
                        i++;
                        options.SetOption(InvocationOptions.InvocationSwitch.overrideDestinationSingleFile, args[i]);
                    }else
                    if (args[i].ToLower().Trim().StartsWith("/label"))
                    {
                        i++;
                        string[] avp = args[i].Split('=');
                        if (avp.Length != 2) throw new Exception("Label could not be parsed.");
                        options.SetOption(InvocationOptions.InvocationSwitch.label,avp[0],avp[1]);
                    }
                }
                projectFilename = args[args.Length - 1];
            }
            catch (Exception ex)
            {
                Console.WriteLine("Invalid arguments.\nUse /inpath [path] /outpath [path] /singlefile [filename]");
            }
            Program xCmd = new Program();
            xReplaceCore x = new xReplaceCore(new xReplaceCore.status(xCmd.PrintStatus), new xReplaceCore.output(xCmd.PrintOutput), new xReplaceCore.error(xCmd.PrintError));
            x.Run(projectFilename,options);
        }

        private static int y = 0;
        private static int x = 0;

        public void PrintStatus(int current, int max, bool verbose)
        {
            if (!verbose)
            {
                string msg = "Processing: " + current + " of " + max;
                y = Console.CursorTop;
                x = Console.CursorLeft;
                Console.Write(msg);
                Console.Out.Flush();
                Console.SetCursorPosition(x, y);
            }
        }

        public void PrintOutput(string msg)
        {
            Console.WriteLine(msg);
        }

        public void PrintError(string msg)
        {
            Console.WriteLine(msg);
        }
    }
}
